
import java.awt.*;
import java.awt.event.MouseListener;
import java.awt.image.BufferStrategy;

public class Game extends Canvas implements Runnable, MouseListener {
    public static final float UPDATE_RATE = 60.0f;
    public static final float UPDATE_INTERVAL = Time.SECOND / UPDATE_RATE;
    public static final long IDLE_TIME = 1;
    Thread thread;
    PlayerObject playerObject;
    Maps maps = new Maps();

    public Game() {
        playerObject = new PlayerObject();
        this.addMouseListener(this);
        new Frame(1080, 720, "棋盘游戏", this);
        start();
        playerObject.addPlayer(new Player(0, 0, 0, 0, 2, 2));
        playerObject.addPlayer(new Player(10, 10, 0, 0, 2, 2));
        playerObject.addPlayer(new Player(5, 5, 0, 0, 2, 2));

    }

    @Override
    public void run() {
        long count = 0;
        float delta = 0;
        long lastTime = Time.get();
        while (true) {
            long now = Time.get();
            long elapsedTime = now - lastTime;
            lastTime = now;
            count += elapsedTime;
            boolean render = false;
            delta += (elapsedTime / UPDATE_INTERVAL);
            while (delta > 1) {
                this.tick();
                this.render();
                delta--;
                if (render) {
                } else {
                    render = true;
                }
            }

            if (render) {
            } else {
                try {
                    Thread.sleep(IDLE_TIME);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            if (count >= Time.SECOND) {
                count = 0;
            }

        }

    }

    public synchronized void start() {
        thread = new Thread(this);
        thread.start();
    }

    public synchronized void stop() {
        try {
            thread.join();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void render() {
        BufferStrategy bs = this.getBufferStrategy();
        if (bs == null) {
            this.createBufferStrategy(3);
            return;
        }
        Graphics g = bs.getDrawGraphics();
        g.setColor(Color.white);
        g.fillRect(0, 0, 1080, 720);

        maps.render(g);
        playerObject.render(g);
        g.dispose();
        bs.show();
    }

    private void tick() {
        playerObject.tick();

    }


    public static void main(String args[]) {
        new Game();
    }

    @Override
    public void mouseClicked(java.awt.event.MouseEvent e) {

    }

    @Override
    public void mousePressed(java.awt.event.MouseEvent e) {
        playerObject.playerLinked.get(0).playMove(e);
        playerObject.playerLinked.get(1).playMove(e);
        playerObject.playerLinked.get(2).playMove(e);
    }

    @Override
    public void mouseReleased(java.awt.event.MouseEvent e) {

    }

    @Override
    public void mouseEntered(java.awt.event.MouseEvent e) {

    }

    @Override
    public void mouseExited(java.awt.event.MouseEvent e) {

    }
}
